package be.bonamis.umpire.quiz;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.joda.time.DateTime;

import be.bonamis.umpire.domain.quiz.Answer;
import be.bonamis.umpire.domain.quiz.Question;
import be.bonamis.umpire.domain.quiz.Quiz;
import be.bonamis.umpire.utils.excel.Excel2Java;

public class QuizReader {
	
	public static Quiz getQuizFromFile(String filename){
		Quiz quiz = new Quiz();
		DateTime creationDay = new DateTime();
		quiz.setCreationDay(creationDay);

		List<QuestionVO> list = QuizReader.getQuestionsFromFile(filename);
		for (QuestionVO questionVO : list) {
			Question question = new Question();
			question.setQuestionFr(questionVO.getQuestion());
			String[] temp;
			String str = questionVO.getResponses();
			/* delimiter */
			String delimiter = "-";
			/* given string will be split by the argument delimiter provided. */
			temp = str.split(delimiter);
			/* print substrings */
			// String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
			question.getAnswers().add(
					new Answer(questionVO.getAnswerA(), "", Arrays.asList(temp).contains("A")));
			question.getAnswers().add(
					new Answer(questionVO.getAnswerB(), "", Arrays.asList(temp).contains("B")));
			question.getAnswers().add(
					new Answer(questionVO.getAnswerC(), "", Arrays.asList(temp).contains("C")));
			question.getAnswers().add(
					new Answer(questionVO.getAnswerD(), "", Arrays.asList(temp).contains("D")));
			quiz.getQuestions().add(question);
		}
		return quiz;
	}

	private static List<QuestionVO> getQuestionsFromFile(String filename) {

		Map<String, String> cellMapping = new HashMap<String, String>();
		cellMapping.put("A", "questionId");
		cellMapping.put("B", "question");
		cellMapping.put("C", "answerA");
		cellMapping.put("D", "answerB");
		cellMapping.put("E", "answerC");
		cellMapping.put("F", "answerD");
		cellMapping.put("G", "responses");

		return Excel2Java.getObjectsFromFile(filename, cellMapping, QuestionVO.class);
	}

	public static Quiz getQuizFromStream(byte[] file) {
		Quiz quiz = new Quiz();
		DateTime creationDay = new DateTime();
		quiz.setCreationDay(creationDay);

		List<QuestionVO> list = QuizReader.getQuestionsFromByteArray(file);
		for (QuestionVO questionVO : list) {
			Question question = new Question();
			question.setQuestionFr(questionVO.getQuestion());
			String[] temp;
			String str = questionVO.getResponses();
			/* delimiter */
			String delimiter = "-";
			/* given string will be split by the argument delimiter provided. */
			temp = str.split(delimiter);
			/* print substrings */
			// String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
			question.getAnswers().add(
					new Answer(questionVO.getAnswerA(), "", Arrays.asList(temp).contains("A")));
			question.getAnswers().add(
					new Answer(questionVO.getAnswerB(), "", Arrays.asList(temp).contains("B")));
			question.getAnswers().add(
					new Answer(questionVO.getAnswerC(), "", Arrays.asList(temp).contains("C")));
			question.getAnswers().add(
					new Answer(questionVO.getAnswerD(), "", Arrays.asList(temp).contains("D")));
			quiz.getQuestions().add(question);
		}
		return quiz;
	}

	private static List<QuestionVO> getQuestionsFromByteArray(byte[] file) {
		Map<String, String> cellMapping = new HashMap<String, String>();
		cellMapping.put("A", "questionId");
		cellMapping.put("B", "question");
		cellMapping.put("C", "answerA");
		cellMapping.put("D", "answerB");
		cellMapping.put("E", "answerC");
		cellMapping.put("F", "answerD");
		cellMapping.put("G", "responses");

		return Excel2Java.getObjectsFromByteArray(file, cellMapping, QuestionVO.class);
	}
}
